<?php  
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 
 * 后台上传模型类
 * @author 66diqiu-li
 *
 */
class Admin_Upload_model extends CI_Model {

    /**
     * 生成缩略图 并返回缩略图名称
     * @param $orginal_img 原图名称 相对路径或者是绝对路径 不能为url路径
     * @param $width      缩略图宽度
     * @param $height     缩略图高度
     * @return string     返回缩略图地址
     */
    public function thumbnail_process($orginal_img,$width,$height){
        $orginal_arr = explode('.',$orginal_img);
        $config['image_library'] = 'gd2';
        $config['source_image'] = $orginal_img;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = FALSE;
        $config['width'] = $width;
        $config['height'] = $height;
        $config['quality'] = 100;
        $config['thumb_marker'] = '_thumb';
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();

        $thumb_img = $orginal_arr[0].$config['thumb_marker'].'.'.$orginal_arr[1];
        //生成缩略图
        $this->ftp_upload($thumb_img);
        return $thumb_img;
    }

    /**
     * 文件上传到ftp中
     */
    function ftp_upload($file){
        $this->load->library('ftp');

        //网站FTP配置信息
        $this->config->load('ftp', TRUE);
        $ftp_config = $this->config->config['ftp'];

        //获取路径
        $absolute_path = str_replace('\\', '/', getcwd()).'/';

        //ftp获取
        $this->ftp->connect($ftp_config);

        //远程路径
        $resource_path = '/'.substr($file,0,strrpos($file,'/')).'/';
        $this->dmkdir($resource_path,$this->ftp);

        //本地路径
        $absolute_path = str_replace('\\', '/', getcwd()).'/';
        $orginal_img = $absolute_path.$file;

        //ftp上传
        $status = $this->ftp->upload($absolute_path.$file, $file);
        //删除本地图片
        if($status){
            if(@file_exists($absolute_path.$file) && !$_GET['ignore_unlink']){
                @unlink($absolute_path.$file);
            }
        }else{

        }
        $this->ftp->close();
    }

    /**
     *
     * 批量创建文件夹
     * @param $dir
     * @param $obj
     * @param int $mode
     * @return bool
     */
    function dmkdir($dir,$obj, $mode = 0777)
    {
        if(!is_dir($dir))
        {
            $this->dmkdir(dirname($dir),$obj, $mode);
            $obj->mkdir($dir, $mode);
        }
        return true;
    }

    /**
     * 上传配置文件
     * @return mixed
     */
    function upload_config($path=''){
        if(!$path)$path = './uploads/'.$GLOBALS['sid'].'/'.date('Ymd',now()).'/';
        else $path = './uploads/'.$GLOBALS['sid'].'/'.$path.'/';
        $GLOBALS['config']['uploadallowext'] = isset($GLOBALS['config']['uploadallowext'])?$GLOBALS['config']['uploadallowext']:'jpg|jpeg|gif|bmp|png|doc|docx|xls|xlsx|ppt|pptx|pdf|txt|rar|zip|swf|ico';
        $GLOBALS['config']['uploadmaxsize'] = isset($GLOBALS['config']['uploadmaxsize'])?$GLOBALS['config']['uploadmaxsize']:'20240';
        $config['upload_path'] = $path;
        $config['allowed_types'] = $GLOBALS['config']['uploadallowext'];
        $config['overwrite'] = FALSE;
        $config['max_size'] = $GLOBALS['config']['uploadmaxsize'];
        $config['max_width'] = '0';
        $config['max_height'] = '0';
        $config['max_filename'] = '0';
        $config['encrypt_name'] = TRUE;
        $config['remove_spaces'] = TRUE;
        if (!is_dir($config['upload_path'])){ dmkdir($config['upload_path'], 0777);};
        return $config;
    }
}